<?php
declare (strict_types = 1);

namespace app\admin\controller;
use app\admin\BaseController;
use app\admin\logic\VipLogic;
use app\admin\model\User;
use think\exception\ValidateException;
use think\facade\Request;
use think\facade\View;

class Vip extends BaseController
{
    public function vipEdit()
    {
        $id=Request::get('id','');
        if (!empty($id)){
            if (Request::isPost()){
                $uname=Request::post('uname');
                $password=Request::post('pwd');
                $pwd2=Request::post('pwd2');
                $sex=Request::post('sex');
                $tel=Request::post('tel');
                $email=Request::post('email');

                try{
                    validate(\app\admin\validate\User::class)->batch(true)->sceneEdit()
                        ->check([
                            'unames'=>$uname,
                            'password'=>md5($password),
                            'repassword'=>$pwd2,
                            'email' =>$email
                        ]);

                }catch (ValidateException $e){
                    $msg = $e->getMessage();
                }

                if (isset($msg)) $this->error($msg);

                $data=[
                    'uname'=>$uname,
                    'password'=>md5($password),
                    'sex'=>$sex,
                    'tel'=>$tel,
                    'email'=>$email
                ];
                $pass=User::where('password',md5($password))->find($id);

                if (empty($pass)){
                    $edit=User::where('id',$id)->update($data);
                    if ($edit){
                        $this->success('修改成功',url('Vip/vipList'));
                    }else{
                        $this->error('修改失败');
                    }
                }else{
                    $this->error('密码与原密码相同');
                }
            }
            $value=User::find($id);

            View::assign('value',$value);
        }

        return view();
    }
    public function vipAdd()
    {
        if (Request::isPost()){
            $uname=Request::post('uname');
            $password=Request::post('pwd');
            $pwd2=Request::post('pwd2');
            $sex=Request::post('sex');
            $tel=Request::post('tel');
            $email=Request::post('email');

            try{
                validate(\app\admin\validate\User::class)->batch(true)
                    ->check([
                        'uname'=>$uname,
                        'password'=>$password,
                        'repassword'=>$pwd2,
                        'email' =>$email
                    ]);

            }catch (ValidateException $e){
                $msg = $e->getMessage();
            }

            if (isset($msg)) $this->error($msg);


            $data=[
                'uname'=>$uname,
                'password'=>md5($password),
                'sex'=>$sex,
                'tel'=>$tel,
                'email'=>$email,
                'addtime'=>time()
            ];

            $add=User::create($data);
            if ($add){
                $this->success('添加成功',url('Vip/vipList'));
            }else{
                $this->error('网络波动添加失败');
            }
        }
        return view();
    }

    public function vipList()
    {

        $where=VipLogic::getWhere();
        list($list_vipList,$page)=User::getVipList($where);
        View::assign('list',['list'=>$list_vipList,'page'=>$page]);
//        unset(Request::param()['page']);
//        echo "<pre>";
//        print_r(Request::param());
//        echo "</pre>";
        return view();
    }


}